//
//  UIView+ILGUIKit.m
//  WangHuo
//
//  Created by zhangtongle on 2017/6/16.
//  Copyright © 2017年 ifeng. All rights reserved.
//

#import "UIView+ILGUIKit.h"
#import "UIView+Toast.h"


@interface UIView ()
@property (class, nonatomic, strong) UIWindow *tempWindow;
@end


@implementation UIView (ILGUIKit)

+ (instancetype)ilg_viewFromXIB:(NSString *)xibName {
    return [[NSBundle mainBundle] loadNibNamed:xibName owner:nil options:nil].firstObject;
}

- (void)ilg_handleTapEventForMovingView:(NSSet<UITouch *> *)touches callback:(void(^)(UIView *movingView))callback {
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self.superview];

    [self ilg_handleTapEventForMovingViewWithTouchPoint:location callback:callback];
}

- (void)ilg_handleTapEventForMovingViewWithTouchPoint:(CGPoint)touchPointInSuperView callback:(void(^)(UIView *movingView))callback {
    CGRect rect = self.layer.presentationLayer.frame;
    
    if (CGRectContainsPoint(rect, touchPointInSuperView)) {
        if (callback) {
            callback(self);
        }
    }
}


#pragma mark - ILGUIKit_frame

- (CGFloat)ilg_x {
    return self.frame.origin.x;
}
- (void)setIlg_x:(CGFloat)ilg_x {
    CGRect frame = self.frame;
    frame.origin.x = ilg_x;
    self.frame = frame;
}

- (CGFloat)ilg_y {
    return self.frame.origin.y;
}
- (void)setIlg_y:(CGFloat)ilg_y {
    CGRect frame = self.frame;
    frame.origin.y = ilg_y;
    self.frame = frame;
}

- (CGFloat)ilg_width {
    return self.frame.size.width;
}
- (void)setIlg_width:(CGFloat)ilg_width {
    [self setIlg_size:CGSizeMake(ilg_width, self.frame.size.height)];
}

- (CGFloat)ilg_height {
    return self.frame.size.height;
}
- (void)setIlg_height:(CGFloat)ilg_height {
    [self setIlg_size:CGSizeMake(self.frame.size.width, ilg_height)];
}

- (CGFloat)ilg_leading {
    return self.ilg_x;
}
- (void)setIlg_leading:(CGFloat)ilg_leading {
    self.ilg_x = ilg_leading;
}

- (CGFloat)ilg_tailing {
    return self.frame.origin.x + self.frame.size.width;
}
- (void)setIlg_tailing:(CGFloat)ilg_tailing {
    self.ilg_x = ilg_tailing - self.frame.size.width;
}

- (CGFloat)ilg_top {
    return self.ilg_y;
}
- (void)setIlg_top:(CGFloat)ilg_top {
    self.ilg_y = ilg_top;
}

- (CGFloat)ilg_bottom {
    return self.frame.origin.y + self.frame.size.height;
}
- (void)setIlg_bottom:(CGFloat)ilg_bottom {
    self.ilg_y = ilg_bottom - self.frame.size.height;
}

- (CGSize)ilg_size {
    return self.frame.size;
}
- (void)setIlg_size:(CGSize)ilg_size {
    CGRect frame = self.frame;
    frame.size = ilg_size;
    self.frame = frame;
}

- (CGFloat)ilg_centerX {
    return self.center.x;
}
- (void)setIlg_centerX:(CGFloat)ilg_centerX {
    CGPoint center = self.center;
    center.x = ilg_centerX;
    self.center = center;
}

- (CGSize)screenSize {
    return [UIScreen mainScreen].bounds.size;
}
- (CGFloat)screenWidth {
    return self.screenSize.width;
}
- (CGFloat)screenHeight {
    return self.screenSize.height;
}


#pragma mark - Toast

- (void)ilg_makeToast:(NSString *)message {
    [self ilg_makeToast:message position:ILGToastPositionCenter];
}

- (void)ilg_makeToast:(NSString *)message position:(ILGToastPosition)position {
    const NSString *positionStr = CSToastPositionCenter;
    if (position == ILGToastPositionTop) {
        positionStr = CSToastPositionTop;
    } else if (position == ILGToastPositionBottom) {
        positionStr = CSToastPositionBottom;
    }
    CSToastStyle *toastStyle = [[CSToastStyle alloc] initWithDefaultStyle];
    
    [self makeToast:message duration:0.5 position:positionStr style:toastStyle];
}

+ (void)ilg_makeToast:(NSString *)message {
    [[UIApplication sharedApplication].keyWindow ilg_makeToast:message];
}

+ (void)ilg_makeToast:(NSString *)message position:(ILGToastPosition)position {
    [[UIApplication sharedApplication].keyWindow ilg_makeToast:message position:position];
}


#pragma mark - TopAlert

+ (void)setTempWindow:(UIWindow *)tempWindow {
    objc_setAssociatedObject([self class], @selector(tempWindow), tempWindow, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

+ (UIWindow *)tempWindow {
    return objc_getAssociatedObject([self class], _cmd);
}

+ (void)ilg_showTopAlertWithText:(NSString *)text autoHide:(BOOL)isAuthHide {    
    if (!self.tempWindow) {
        UIWindow *tempWindow = [[UIWindow alloc]init];
        tempWindow.frame = CGRectMake(0, -50, [UIScreen mainScreen].bounds.size.width, 50);
        tempWindow.windowLevel = UIWindowLevelAlert;
        tempWindow.backgroundColor = [UIColor redColor];
        tempWindow.hidden = NO;
        self.tempWindow = tempWindow;
        
        UIButton *topView = [UIButton buttonWithType:UIButtonTypeCustom];
        topView.backgroundColor = [UIColor clearColor];
        [topView setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        topView.titleLabel.font = [UIFont systemFontOfSize:15];
        [topView setTitle:text forState:UIControlStateNormal];
        [topView addTarget:self action:@selector(ilg_hideTopAlert) forControlEvents:UIControlEventTouchUpInside];
        topView.frame = tempWindow.bounds;
        
        [tempWindow addSubview:topView];
    } else {
        NSArray *subViews = self.tempWindow.subviews;
        for (UIView *view in subViews) {
            if ([view isKindOfClass:[UIButton class]]) {
                [((UIButton *)view) setTitle:text forState:UIControlStateNormal];
                break;
            }
        }
    }
    
    [UIView animateWithDuration:0.26 animations:^{
        CGRect frame = self.tempWindow.frame;
        frame.origin.y = 0;
        
        self.tempWindow.frame = frame;
    }];
    
    if (isAuthHide) {
        [NSTimer scheduledTimerWithTimeInterval:3.0 repeats:NO block:^(NSTimer * _Nonnull timer) {
            [self ilg_hideTopAlert];
        }];
    }
}

+ (void)ilg_showTopAlertWithText:(NSString *)text {
    [self ilg_showTopAlertWithText:text autoHide:YES];
}

+ (void)ilg_hideTopAlert {
    [UIView animateWithDuration:0.26 animations:^{
        CGRect frame = self.tempWindow.frame;
        frame.origin.y = - self.tempWindow.frame.size.height;
        self.tempWindow.frame = frame;
    }completion:^(BOOL finished) {
        NSLog(@"%@", self.tempWindow);
        objc_removeAssociatedObjects([self class]);
    }];
}

@end
